package com.tehui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.tehui.R;
import com.tehui.activity.impl.WebActivity;
import com.tehui.utils.ui.UiUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.tehui.app.MyApplication.IS_TAOKE;

public abstract class BaseActivity extends AppCompatActivity {

    private boolean isActive = true;
    private String format;

    //共有的
    @BindView(R.id.goBack)
    public ImageView      goBack;
    @BindView(R.id.tvTitle)
    public TextView       tvTitle;
    public RequestOptions options;

    //基类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(provideContentViewId());
        ButterKnife.bind(this);
        initActivityData();
        initViewListener();
        //初始化友盟统计
        MobclickAgent.setScenarioType(UiUtils.getContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);
        MobclickAgent.setCatchUncaughtExceptions(false);
        //设置状态栏颜色
        //StatusBarUtil.setColor(this, -438935, 0);

        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        initGlide();
    }

    public void initGlide() {
        if (IS_TAOKE) {
            options = new RequestOptions().centerCrop().placeholder(R.drawable.sd);
        } else {
            options = new RequestOptions().centerCrop().placeholder(R.drawable.sd1);
        }
    }


    public void initViewListener() {

    }

    public void initActivityData() {
    }

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //if (!isAppOnForeground()) {
        //    //app 进入后台
        //    isActive = false;
        //}
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        //        SimpleDateFormat mm = new SimpleDateFormat("mm");
        //        Date curDate = new Date(System.currentTimeMillis());
        //        format = mm.format(curDate);
        //

        //        if (!isActive) {
        //            if (format.equals("00") || format.equals("10") || format.equals("20") || format
        // .equals("30") ||
        //                    format.equals("40") || format.equals("50") || format.equals("55")) {
        //                //app 从后台唤醒，进入前台
        //                isActive = true;
        //                Intent intent = new Intent(this, SplashActivity.class);
        //                intent.putExtra("stage", true);
        //                startActivity(intent);
        //            }
        //        }
    }

    /**
     * 程序是否在前台运行
     * @return
     */
    //    public boolean isAppOnForeground() {
    //        // Returns a list of application processes that are running on the
    //        // device
    //        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService
    //                (Context.ACTIVITY_SERVICE);
    //        String packageName = getApplicationContext().getPackageName();
    //
    //        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
    // .getRunningAppProcesses();
    //        if (appProcesses == null)
    //            return false;
    //
    //        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
    //            // The name of the process that this object is associated with.
    //            if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager
    //                    .RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
    //                return true;
    //            }
    //        }
    //        return false;
    //    }
    public void jumpToActivity(Intent intent) {
        startActivity(intent);
    }

    public void jumpToActivity(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    public void jumpToWebViewActivity(String url) {
        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra("url", url);
        jumpToActivity(intent);
    }
}



